<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>Middleware | Redux 中文文档 Join the chat at https://gitter.im/camsong/redux-in-chinese</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.3.3">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-edit-link/plugin.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
    

        
    
    
    <link rel="next" href="../../docs/advanced/UsageWithReactRouter.html" />
    
    
    <link rel="prev" href="../../docs/advanced/AsyncFlow.html" />
    

        
    </head>
    <body>
        
        
    <div class="book" data-level="3.3" data-basepath="../.." data-revision="Thu Mar 16 2017 23:41:56 GMT+0800 (CST)">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
        
        
        

        

        
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        自述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="docs/introduction/index.html">
            
                
                    <a href="../../docs/introduction/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        介绍
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="docs/introduction/Motivation.html">
            
                
                    <a href="../../docs/introduction/Motivation.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        动机
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="docs/introduction/CoreConcepts.html">
            
                
                    <a href="../../docs/introduction/CoreConcepts.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        核心概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="docs/introduction/ThreePrinciples.html">
            
                
                    <a href="../../docs/introduction/ThreePrinciples.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        三大原则
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="docs/introduction/PriorArt.html">
            
                
                    <a href="../../docs/introduction/PriorArt.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        先前技术
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="docs/introduction/Ecosystem.html">
            
                
                    <a href="../../docs/introduction/Ecosystem.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        生态系统
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="docs/introduction/Examples.html">
            
                
                    <a href="../../docs/introduction/Examples.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        示例
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="docs/basics/index.html">
            
                
                    <a href="../../docs/basics/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        基础
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="docs/basics/Actions.html">
            
                
                    <a href="../../docs/basics/Actions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        Action
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="docs/basics/Reducers.html">
            
                
                    <a href="../../docs/basics/Reducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        Reducer
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="docs/basics/Store.html">
            
                
                    <a href="../../docs/basics/Store.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        Store
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="docs/basics/DataFlow.html">
            
                
                    <a href="../../docs/basics/DataFlow.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        数据流
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="docs/basics/UsageWithReact.html">
            
                
                    <a href="../../docs/basics/UsageWithReact.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        搭配 React
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="docs/basics/ExampleTodoList.html">
            
                
                    <a href="../../docs/basics/ExampleTodoList.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        示例：Todo List
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="docs/advanced/index.html">
            
                
                    <a href="../../docs/advanced/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="docs/advanced/AsyncActions.html">
            
                
                    <a href="../../docs/advanced/AsyncActions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        异步 Action
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="docs/advanced/AsyncFlow.html">
            
                
                    <a href="../../docs/advanced/AsyncFlow.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        异步数据流
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="3.3" data-path="docs/advanced/Middleware.html">
            
                
                    <a href="../../docs/advanced/Middleware.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        Middleware
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="docs/advanced/UsageWithReactRouter.html">
            
                
                    <a href="../../docs/advanced/UsageWithReactRouter.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        搭配 React Router
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="docs/advanced/ExampleRedditAPI.html">
            
                
                    <a href="../../docs/advanced/ExampleRedditAPI.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        示例：Reddit API
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="docs/advanced/NextSteps.html">
            
                
                    <a href="../../docs/advanced/NextSteps.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        下一步
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="docs/recipes/index.html">
            
                
                    <a href="../../docs/recipes/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        技巧
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="docs/recipes/MigratingToRedux.html">
            
                
                    <a href="../../docs/recipes/MigratingToRedux.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        迁移到 Redux
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="docs/recipes/UsingObjectSpreadOperator.html">
            
                
                    <a href="../../docs/recipes/UsingObjectSpreadOperator.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        使用对象展开运算符
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="docs/recipes/ReducingBoilerplate.html">
            
                
                    <a href="../../docs/recipes/ReducingBoilerplate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        减少样板代码
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="docs/recipes/ServerRendering.html">
            
                
                    <a href="../../docs/recipes/ServerRendering.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        服务端渲染
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="docs/recipes/WritingTests.html">
            
                
                    <a href="../../docs/recipes/WritingTests.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        编写测试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="docs/recipes/ComputingDerivedData.html">
            
                
                    <a href="../../docs/recipes/ComputingDerivedData.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        计算衍生数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="docs/recipes/ImplementingUndoHistory.html">
            
                
                    <a href="../../docs/recipes/ImplementingUndoHistory.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                        实现撤销重做
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="docs/recipes/IsolatingSubapps.html">
            
                
                    <a href="../../docs/recipes/IsolatingSubapps.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.8.</b>
                        
                        子应用隔离
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9" data-path="docs/recipes/StructuringReducers.html">
            
                
                    <a href="../../docs/recipes/StructuringReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.</b>
                        
                        组织 Reducer
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.9.1" data-path="docs/recipes/reducers/PrerequisiteConcepts.html">
            
                
                    <a href="../../docs/recipes/reducers/PrerequisiteConcepts.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.1.</b>
                        
                        Reducer 基础概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.2" data-path="docs/recipes/reducers/BasicReducerStructure.html">
            
                
                    <a href="../../docs/recipes/reducers/BasicReducerStructure.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.2.</b>
                        
                        Reducer 基础结构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.3" data-path="docs/recipes/reducers/SplittingReducerLogic.html">
            
                
                    <a href="../../docs/recipes/reducers/SplittingReducerLogic.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.3.</b>
                        
                        Reducer 逻辑拆分
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.4" data-path="docs/recipes/reducers/RefactoringReducersExample.html">
            
                
                    <a href="../../docs/recipes/reducers/RefactoringReducersExample.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.4.</b>
                        
                        Reducer 重构示例
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.5" data-path="docs/recipes/reducers/UsingCombineReducers.html">
            
                
                    <a href="../../docs/recipes/reducers/UsingCombineReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.5.</b>
                        
                        `combineReducers` 用法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.6" data-path="docs/recipes/reducers/BeyondCombineReducers.html">
            
                
                    <a href="../../docs/recipes/reducers/BeyondCombineReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.6.</b>
                        
                        `combineReducers` 进阶
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.7" data-path="docs/recipes/reducers/NormalizingStateShape.html">
            
                
                    <a href="../../docs/recipes/reducers/NormalizingStateShape.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.7.</b>
                        
                        State 范式化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.8" data-path="docs/recipes/reducers/UpdatingNormalizedData.html">
            
                
                    <a href="../../docs/recipes/reducers/UpdatingNormalizedData.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.8.</b>
                        
                        管理范式化数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.9" data-path="docs/recipes/reducers/ReusingReducerLogic.html">
            
                
                    <a href="../../docs/recipes/reducers/ReusingReducerLogic.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.9.</b>
                        
                        Reducer 逻辑复用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.10" data-path="docs/recipes/reducers/ImmutableUpdatePatterns.html">
            
                
                    <a href="../../docs/recipes/reducers/ImmutableUpdatePatterns.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.10.</b>
                        
                        不可变更新模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.11" data-path="docs/recipes/reducers/InitializingState.html">
            
                
                    <a href="../../docs/recipes/reducers/InitializingState.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.11.</b>
                        
                        初始化 State
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="docs/FAQ.html">
            
                
                    <a href="../../docs/FAQ.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        常见问题
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="docs/faq/General.html">
            
                
                    <a href="../../docs/faq/General.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        综合
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="docs/faq/Reducers.html">
            
                
                    <a href="../../docs/faq/Reducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        Reducer
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="docs/faq/OrganizingState.html">
            
                
                    <a href="../../docs/faq/OrganizingState.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        组织 State
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="docs/faq/StoreSetup.html">
            
                
                    <a href="../../docs/faq/StoreSetup.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        创建 Store
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="docs/faq/Actions.html">
            
                
                    <a href="../../docs/faq/Actions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        Action
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="docs/faq/CodeStructure.html">
            
                
                    <a href="../../docs/faq/CodeStructure.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        代码结构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="docs/faq/Performance.html">
            
                
                    <a href="../../docs/faq/Performance.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        性能
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="docs/faq/ReactRedux.html">
            
                
                    <a href="../../docs/faq/ReactRedux.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.8.</b>
                        
                        React Redux
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.9" data-path="docs/faq/Miscellaneous.html">
            
                
                    <a href="../../docs/faq/Miscellaneous.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.9.</b>
                        
                        其它
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="docs/Troubleshooting.html">
            
                
                    <a href="../../docs/Troubleshooting.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        排错
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="docs/Glossary.html">
            
                
                    <a href="../../docs/Glossary.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        词汇表
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="docs/api/index.html">
            
                
                    <a href="../../docs/api/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        API 文档
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="docs/api/createStore.html">
            
                
                    <a href="../../docs/api/createStore.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        createStore
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="docs/api/Store.html">
            
                
                    <a href="../../docs/api/Store.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        Store
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="docs/api/combineReducers.html">
            
                
                    <a href="../../docs/api/combineReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        combineReducers
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="docs/api/applyMiddleware.html">
            
                
                    <a href="../../docs/api/applyMiddleware.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        applyMiddleware
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.5" data-path="docs/api/bindActionCreators.html">
            
                
                    <a href="../../docs/api/bindActionCreators.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                        bindActionCreators
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.6" data-path="docs/api/compose.html">
            
                
                    <a href="../../docs/api/compose.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.6.</b>
                        
                        compose
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="9" data-path="docs/react-redux/index.html">
            
                
                    <a href="../../docs/react-redux/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        react-redux 文档
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1" data-path="docs/react-redux/api.html">
            
                
                    <a href="../../docs/react-redux/api.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                        API
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="docs/react-redux/troubleshooting.html">
            
                
                    <a href="../../docs/react-redux/troubleshooting.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.2.</b>
                        
                        排错
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10" >
            
                
                    <a target="_blank" href="https:/github.com/react-guide/redux-tutorial-cn">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        redux-tutorial
                    </a>
            
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                Published with GitBook
            </a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Table of Contents"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Font Settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Share"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">
                    Share on Twitter
                </button>
                <button type="button" data-sharing="google-plus" class="button">
                    Share on Google
                </button>
                <button type="button" data-sharing="facebook" class="button">
                    Share on Facebook
                </button>
                <button type="button" data-sharing="weibo" class="button">
                    Share on Weibo
                </button>
                <button type="button" data-sharing="instapaper" class="button">
                    Share on Instapaper
                </button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Google"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Twitter"><i class="fa fa-twitter"></i></a>
    
    
    


    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >Redux 中文文档 Join the chat at https://gitter.im/camsong/redux-in-chinese</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <a id="edit-link" href="https://github.com/camsong/redux-in-chinese/tree/master/docs/advanced/Middleware.md" class="btn fa fa-edit pull-left">&#xA0;&#xA0;&#x5F00;&#x59CB;&#x7EA0;&#x9519;</a><h1 id="middleware">Middleware</h1>
<p>&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x5728;<a href="AsyncActions.html">&#x5F02;&#x6B65; Action </a>&#x4E00;&#x8282;&#x7684;&#x793A;&#x4F8B;&#x4E2D;&#x770B;&#x5230;&#x4E86;&#x4E00;&#x4E9B; middleware &#x7684;&#x4F7F;&#x7528;&#x3002;&#x5982;&#x679C;&#x4F60;&#x4F7F;&#x7528;&#x8FC7; <a href="http://expressjs.com/" target="_blank">Express</a> &#x6216;&#x8005; <a href="http://koajs.com/" target="_blank">Koa</a> &#x7B49;&#x670D;&#x52A1;&#x7AEF;&#x6846;&#x67B6;, &#x90A3;&#x4E48;&#x5E94;&#x8BE5;&#x5BF9; <em>middleware</em> &#x7684;&#x6982;&#x5FF5;&#x4E0D;&#x4F1A;&#x964C;&#x751F;&#x3002; &#x5728;&#x8FD9;&#x7C7B;&#x6846;&#x67B6;&#x4E2D;&#xFF0C;middleware &#x662F;&#x6307;&#x53EF;&#x4EE5;&#x88AB;&#x5D4C;&#x5165;&#x5728;&#x6846;&#x67B6;&#x63A5;&#x6536;&#x8BF7;&#x6C42;&#x5230;&#x4EA7;&#x751F;&#x54CD;&#x5E94;&#x8FC7;&#x7A0B;&#x4E4B;&#x4E2D;&#x7684;&#x4EE3;&#x7801;&#x3002;&#x4F8B;&#x5982;&#xFF0C;Express &#x6216;&#x8005; Koa &#x7684; middleware &#x53EF;&#x4EE5;&#x5B8C;&#x6210;&#x6DFB;&#x52A0; CORS headers&#x3001;&#x8BB0;&#x5F55;&#x65E5;&#x5FD7;&#x3001;&#x5185;&#x5BB9;&#x538B;&#x7F29;&#x7B49;&#x5DE5;&#x4F5C;&#x3002;middleware &#x6700;&#x4F18;&#x79C0;&#x7684;&#x7279;&#x6027;&#x5C31;&#x662F;&#x53EF;&#x4EE5;&#x88AB;&#x94FE;&#x5F0F;&#x7EC4;&#x5408;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x4E00;&#x4E2A;&#x9879;&#x76EE;&#x4E2D;&#x4F7F;&#x7528;&#x591A;&#x4E2A;&#x72EC;&#x7ACB;&#x7684;&#x7B2C;&#x4E09;&#x65B9; middleware&#x3002;</p>
<p>&#x76F8;&#x5BF9;&#x4E8E; Express &#x6216;&#x8005; Koa &#x7684; middleware&#xFF0C;Redux middleware &#x88AB;&#x7528;&#x4E8E;&#x89E3;&#x51B3;&#x4E0D;&#x540C;&#x7684;&#x95EE;&#x9898;&#xFF0C;&#x4F46;&#x5176;&#x4E2D;&#x7684;&#x6982;&#x5FF5;&#x662F;&#x7C7B;&#x4F3C;&#x7684;&#x3002;<strong>&#x5B83;&#x63D0;&#x4F9B;&#x7684;&#x662F;&#x4F4D;&#x4E8E; action &#x88AB;&#x53D1;&#x8D77;&#x4E4B;&#x540E;&#xFF0C;&#x5230;&#x8FBE; reducer &#x4E4B;&#x524D;&#x7684;&#x6269;&#x5C55;&#x70B9;&#x3002;</strong> &#x4F60;&#x53EF;&#x4EE5;&#x5229;&#x7528; Redux middleware &#x6765;&#x8FDB;&#x884C;&#x65E5;&#x5FD7;&#x8BB0;&#x5F55;&#x3001;&#x521B;&#x5EFA;&#x5D29;&#x6E83;&#x62A5;&#x544A;&#x3001;&#x8C03;&#x7528;&#x5F02;&#x6B65;&#x63A5;&#x53E3;&#x6216;&#x8005;&#x8DEF;&#x7531;&#x7B49;&#x7B49;&#x3002;</p>
<p>&#x8FD9;&#x4E2A;&#x7AE0;&#x8282;&#x5206;&#x4E3A;&#x4E24;&#x4E2A;&#x90E8;&#x5206;&#xFF0C;&#x524D;&#x9762;&#x662F;&#x5E2E;&#x52A9;&#x4F60;&#x7406;&#x89E3;&#x76F8;&#x5173;&#x6982;&#x5FF5;&#x7684;&#x6DF1;&#x5EA6;&#x4ECB;&#x7ECD;&#xFF0C;&#x800C;&#x540E;&#x534A;&#x90E8;&#x5206;&#x5219;&#x901A;&#x8FC7;<a href="#seven-examples">&#x4E00;&#x4E9B;&#x5B9E;&#x4F8B;</a>&#x6765;&#x4F53;&#x73B0; middleware &#x7684;&#x5F3A;&#x5927;&#x80FD;&#x529B;&#x3002;&#x5BF9;&#x6587;&#x7AE0;&#x524D;&#x540E;&#x5185;&#x5BB9;&#x8FDB;&#x884C;&#x7ED3;&#x5408;&#x901A;&#x8BFB;&#xFF0C;&#x4F1A;&#x5E2E;&#x52A9;&#x4F60;&#x66F4;&#x597D;&#x7684;&#x7406;&#x89E3;&#x67AF;&#x71E5;&#x7684;&#x6982;&#x5FF5;&#xFF0C;&#x5E76;&#x4ECE;&#x4E2D;&#x83B7;&#x5F97;&#x542F;&#x53D1;&#x3002;</p>
<h2 id="%E7%90%86%E8%A7%A3-middleware">&#x7406;&#x89E3; Middleware</h2>
<p>&#x6B63;&#x56E0;&#x4E3A; middleware &#x53EF;&#x4EE5;&#x5B8C;&#x6210;&#x5305;&#x62EC;&#x5F02;&#x6B65; API &#x8C03;&#x7528;&#x5728;&#x5185;&#x7684;&#x5404;&#x79CD;&#x4E8B;&#x60C5;&#xFF0C;&#x4E86;&#x89E3;&#x5B83;&#x7684;&#x6F14;&#x5316;&#x8FC7;&#x7A0B;&#x662F;&#x4E00;&#x4EF6;&#x76F8;&#x5F53;&#x91CD;&#x8981;&#x7684;&#x4E8B;&#x3002;&#x6211;&#x4EEC;&#x5C06;&#x4EE5;&#x8BB0;&#x5F55;&#x65E5;&#x5FD7;&#x548C;&#x521B;&#x5EFA;&#x5D29;&#x6E83;&#x62A5;&#x544A;&#x4E3A;&#x4F8B;&#xFF0C;&#x5F15;&#x5BFC;&#x4F60;&#x4F53;&#x4F1A;&#x4ECE;&#x5206;&#x6790;&#x95EE;&#x9898;&#x5230;&#x901A;&#x8FC7;&#x6784;&#x5EFA; middleware &#x89E3;&#x51B3;&#x95EE;&#x9898;&#x7684;&#x601D;&#x7EF4;&#x8FC7;&#x7A0B;&#x3002;</p>
<h3 id="%E9%97%AE%E9%A2%98-%E8%AE%B0%E5%BD%95%E6%97%A5%E5%BF%97">&#x95EE;&#x9898;: &#x8BB0;&#x5F55;&#x65E5;&#x5FD7;</h3>
<p>&#x4F7F;&#x7528; Redux &#x7684;&#x4E00;&#x4E2A;&#x76CA;&#x5904;&#x5C31;&#x662F;&#x5B83;&#x8BA9; state &#x7684;&#x53D8;&#x5316;&#x8FC7;&#x7A0B;&#x53D8;&#x7684;&#x53EF;&#x9884;&#x77E5;&#x548C;&#x900F;&#x660E;&#x3002;&#x6BCF;&#x5F53;&#x4E00;&#x4E2A; action &#x53D1;&#x8D77;&#x5B8C;&#x6210;&#x540E;&#xFF0C;&#x65B0;&#x7684; state &#x5C31;&#x4F1A;&#x88AB;&#x8BA1;&#x7B97;&#x5E76;&#x4FDD;&#x5B58;&#x4E0B;&#x6765;&#x3002;State &#x4E0D;&#x80FD;&#x88AB;&#x81EA;&#x8EAB;&#x4FEE;&#x6539;&#xFF0C;&#x53EA;&#x80FD;&#x7531;&#x7279;&#x5B9A;&#x7684; action &#x5F15;&#x8D77;&#x53D8;&#x5316;&#x3002;</p>
<p>&#x8BD5;&#x60F3;&#x4E00;&#x4E0B;&#xFF0C;&#x5F53;&#x6211;&#x4EEC;&#x7684;&#x5E94;&#x7528;&#x4E2D;&#x6BCF;&#x4E00;&#x4E2A; action &#x88AB;&#x53D1;&#x8D77;&#x4EE5;&#x53CA;&#x6BCF;&#x6B21;&#x65B0;&#x7684; state &#x88AB;&#x8BA1;&#x7B97;&#x5B8C;&#x6210;&#x65F6;&#x90FD;&#x5C06;&#x5B83;&#x4EEC;&#x8BB0;&#x5F55;&#x4E0B;&#x6765;&#xFF0C;&#x5C82;&#x4E0D;&#x662F;&#x5F88;&#x597D;&#xFF1F;&#x5F53;&#x7A0B;&#x5E8F;&#x51FA;&#x73B0;&#x95EE;&#x9898;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x67E5;&#x9605;&#x65E5;&#x5FD7;&#x627E;&#x51FA;&#x662F;&#x54EA;&#x4E2A; action &#x5BFC;&#x81F4;&#x4E86; state &#x4E0D;&#x6B63;&#x786E;&#x3002;</p>
<p><img src="http://i.imgur.com/BjGBlES.png" width="70%"></p>
<p>&#x6211;&#x4EEC;&#x5982;&#x4F55;&#x901A;&#x8FC7; Redux &#x5B9E;&#x73B0;&#x5B83;&#x5462;&#xFF1F;</p>
<h3 id="%E5%B0%9D%E8%AF%95-1-%E6%89%8B%E5%8A%A8%E8%AE%B0%E5%BD%95">&#x5C1D;&#x8BD5; #1: &#x624B;&#x52A8;&#x8BB0;&#x5F55;</h3>
<p>&#x6700;&#x76F4;&#x63A5;&#x7684;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#x5C31;&#x662F;&#x5728;&#x6BCF;&#x6B21;&#x8C03;&#x7528; <a href="../api/Store.html#dispatch"><code>store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span></code></a> &#x524D;&#x540E;&#x624B;&#x52A8;&#x8BB0;&#x5F55;&#x88AB;&#x53D1;&#x8D77;&#x7684; action &#x548C;&#x65B0;&#x7684; state&#x3002;&#x8FD9;&#x79F0;&#x4E0D;&#x4E0A;&#x4E00;&#x4E2A;&#x771F;&#x6B63;&#x7684;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#xFF0C;&#x4EC5;&#x4EC5;&#x662F;&#x6211;&#x4EEC;&#x7406;&#x89E3;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#x7684;&#x7B2C;&#x4E00;&#x6B65;&#x3002;</p>
<blockquote>
<h5 id="%E6%B3%A8%E6%84%8F">&#x6CE8;&#x610F;</h5>
<p>&#x5982;&#x679C;&#x4F60;&#x4F7F;&#x7528; <a href="https://github.com/gaearon/react-redux" target="_blank">react-redux</a> &#x6216;&#x8005;&#x7C7B;&#x4F3C;&#x7684;&#x7ED1;&#x5B9A;&#x5E93;&#xFF0C;&#x6700;&#x597D;&#x4E0D;&#x8981;&#x76F4;&#x63A5;&#x5728;&#x4F60;&#x7684;&#x7EC4;&#x4EF6;&#x4E2D;&#x64CD;&#x4F5C; store &#x7684;&#x5B9E;&#x4F8B;&#x3002;&#x5728;&#x63A5;&#x4E0B;&#x6765;&#x7684;&#x5185;&#x5BB9;&#x4E2D;&#xFF0C;&#x4EC5;&#x4EC5;&#x662F;&#x5047;&#x8BBE;&#x4F60;&#x4F1A;&#x901A;&#x8FC7; store &#x663E;&#x5F0F;&#x5730;&#x5411;&#x4E0B;&#x4F20;&#x9012;&#x3002;</p>
</blockquote>
<p>&#x5047;&#x8BBE;&#xFF0C;&#x4F60;&#x5728;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; Todo &#x65F6;&#x8FD9;&#x6837;&#x8C03;&#x7528;&#xFF1A;</p>
<pre><code class="lang-js">store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token function">addTodo</span><span class="token punctuation">(</span><span class="token string">&apos;Use Redux&apos;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre>
<p>&#x4E3A;&#x4E86;&#x8BB0;&#x5F55;&#x8FD9;&#x4E2A; action &#x4EE5;&#x53CA;&#x4EA7;&#x751F;&#x7684;&#x65B0;&#x7684; state&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x8BB0;&#x5F55;&#x65E5;&#x5FD7;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token keyword">let</span> action <span class="token operator">=</span> <span class="token function">addTodo</span><span class="token punctuation">(</span><span class="token string">&apos;Use Redux&apos;</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;dispatching&apos;</span><span class="token punctuation">,</span> action<span class="token punctuation">)</span>
store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;next state&apos;</span><span class="token punctuation">,</span> store<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre>
<p>&#x867D;&#x7136;&#x8FD9;&#x6837;&#x505A;&#x8FBE;&#x5230;&#x4E86;&#x60F3;&#x8981;&#x7684;&#x6548;&#x679C;&#xFF0C;&#x4F46;&#x662F;&#x4F60;&#x5E76;&#x4E0D;&#x60F3;&#x6BCF;&#x6B21;&#x90FD;&#x8FD9;&#x4E48;&#x5E72;&#x3002;</p>
<h3 id="%E5%B0%9D%E8%AF%95-2-%E5%B0%81%E8%A3%85-dispatch">&#x5C1D;&#x8BD5; #2: &#x5C01;&#x88C5; Dispatch</h3>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x5C06;&#x4E0A;&#x9762;&#x7684;&#x64CD;&#x4F5C;&#x62BD;&#x53D6;&#x6210;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token keyword">function</span> <span class="token function">dispatchAndLog</span><span class="token punctuation">(</span>store<span class="token punctuation">,</span> action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;dispatching&apos;</span><span class="token punctuation">,</span> action<span class="token punctuation">)</span>
  store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;next state&apos;</span><span class="token punctuation">,</span> store<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x7136;&#x540E;&#x7528;&#x5B83;&#x66FF;&#x6362; <code>store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>:</p>
<pre><code class="lang-js"><span class="token function">dispatchAndLog</span><span class="token punctuation">(</span>store<span class="token punctuation">,</span> <span class="token function">addTodo</span><span class="token punctuation">(</span><span class="token string">&apos;Use Redux&apos;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x9009;&#x62E9;&#x5230;&#x6B64;&#x4E3A;&#x6B62;&#xFF0C;&#x4F46;&#x662F;&#x6BCF;&#x6B21;&#x90FD;&#x8981;&#x5BFC;&#x5165;&#x4E00;&#x4E2A;&#x5916;&#x90E8;&#x65B9;&#x6CD5;&#x603B;&#x5F52;&#x8FD8;&#x662F;&#x4E0D;&#x592A;&#x65B9;&#x4FBF;&#x3002;</p>
<h3 id="%E5%B0%9D%E8%AF%95-3-monkeypatching-dispatch">&#x5C1D;&#x8BD5; #3: Monkeypatching Dispatch</h3>
<p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x76F4;&#x63A5;&#x66FF;&#x6362; store &#x5B9E;&#x4F8B;&#x4E2D;&#x7684; <code>dispatch</code> &#x51FD;&#x6570;&#x4F1A;&#x600E;&#x4E48;&#x6837;&#x5462;&#xFF1F;Redux store &#x53EA;&#x662F;&#x4E00;&#x4E2A;&#x5305;&#x542B;<a href="../api/Store.html">&#x4E00;&#x4E9B;&#x65B9;&#x6CD5;</a>&#x7684;&#x666E;&#x901A;&#x5BF9;&#x8C61;&#xFF0C;&#x540C;&#x65F6;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x7684;&#x662F; JavaScript&#xFF0C;&#x56E0;&#x6B64;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x5B9E;&#x73B0; <code>dispatch</code> &#x7684; monkeypatch&#xFF1A;</p>
<pre><code class="lang-js"><span class="token keyword">let</span> next <span class="token operator">=</span> store<span class="token punctuation">.</span>dispatch
store<span class="token punctuation">.</span>dispatch <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token function">dispatchAndLog</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;dispatching&apos;</span><span class="token punctuation">,</span> action<span class="token punctuation">)</span>
  <span class="token keyword">let</span> result <span class="token operator">=</span> <span class="token function">next</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;next state&apos;</span><span class="token punctuation">,</span> store<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token keyword">return</span> result
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD9;&#x79BB;&#x6211;&#x4EEC;&#x60F3;&#x8981;&#x7684;&#x5DF2;&#x7ECF;&#x975E;&#x5E38;&#x63A5;&#x8FD1;&#x4E86;&#xFF01;&#x65E0;&#x8BBA;&#x6211;&#x4EEC;&#x5728;&#x54EA;&#x91CC;&#x53D1;&#x8D77; action&#xFF0C;&#x4FDD;&#x8BC1;&#x90FD;&#x4F1A;&#x88AB;&#x8BB0;&#x5F55;&#x3002;Monkeypatching &#x4EE4;&#x4EBA;&#x611F;&#x89C9;&#x8FD8;&#x662F;&#x4E0D;&#x592A;&#x8212;&#x670D;&#xFF0C;&#x4E0D;&#x8FC7;&#x5229;&#x7528;&#x5B83;&#x6211;&#x4EEC;&#x505A;&#x5230;&#x4E86;&#x6211;&#x4EEC;&#x60F3;&#x8981;&#x7684;&#x3002;</p>
<h3 id="%E9%97%AE%E9%A2%98-%E5%B4%A9%E6%BA%83%E6%8A%A5%E5%91%8A">&#x95EE;&#x9898;: &#x5D29;&#x6E83;&#x62A5;&#x544A;</h3>
<p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x60F3;&#x5BF9; <code>dispatch</code> &#x9644;&#x52A0;<strong>&#x8D85;&#x8FC7;&#x4E00;&#x4E2A;</strong>&#x7684;&#x53D8;&#x6362;&#xFF0C;&#x53C8;&#x4F1A;&#x600E;&#x4E48;&#x6837;&#x5462;&#xFF1F;</p>
<p>&#x6211;&#x8111;&#x6D77;&#x4E2D;&#x51FA;&#x73B0;&#x7684;&#x53E6;&#x4E00;&#x4E2A;&#x5E38;&#x7528;&#x7684;&#x53D8;&#x6362;&#x5C31;&#x662F;&#x5728;&#x751F;&#x4EA7;&#x8FC7;&#x7A0B;&#x4E2D;&#x62A5;&#x544A; JavaScript &#x7684;&#x9519;&#x8BEF;&#x3002;&#x5168;&#x5C40;&#x7684; <code>window<span class="token punctuation">.</span>onerror</code> &#x5E76;&#x4E0D;&#x53EF;&#x9760;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x5728;&#x4E00;&#x4E9B;&#x65E7;&#x7684;&#x6D4F;&#x89C8;&#x5668;&#x4E2D;&#x65E0;&#x6CD5;&#x63D0;&#x4F9B;&#x9519;&#x8BEF;&#x5806;&#x6808;&#xFF0C;&#x800C;&#x8FD9;&#x662F;&#x6392;&#x67E5;&#x9519;&#x8BEF;&#x6240;&#x9700;&#x7684;&#x81F3;&#x5173;&#x91CD;&#x8981;&#x4FE1;&#x606F;&#x3002;</p>
<p>&#x8BD5;&#x60F3;&#x5F53;&#x53D1;&#x8D77;&#x4E00;&#x4E2A; action &#x7684;&#x7ED3;&#x679C;&#x662F;&#x4E00;&#x4E2A;&#x5F02;&#x5E38;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x5305;&#x542B;&#x8C03;&#x7528;&#x5806;&#x6808;&#xFF0C;&#x5F15;&#x8D77;&#x9519;&#x8BEF;&#x7684; action &#x4EE5;&#x53CA;&#x5F53;&#x524D;&#x7684; state &#x7B49;&#x9519;&#x8BEF;&#x4FE1;&#x606F;&#x901A;&#x901A;&#x53D1;&#x5230;&#x7C7B;&#x4F3C;&#x4E8E; <a href="https://getsentry.com/welcome/" target="_blank">Sentry</a> &#x8FD9;&#x6837;&#x7684;&#x62A5;&#x544A;&#x670D;&#x52A1;&#x4E2D;&#xFF0C;&#x4E0D;&#x662F;&#x5F88;&#x597D;&#x5417;&#xFF1F;&#x8FD9;&#x6837;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x66F4;&#x5BB9;&#x6613;&#x5730;&#x5728;&#x5F00;&#x53D1;&#x73AF;&#x5883;&#x4E2D;&#x91CD;&#x73B0;&#x8FD9;&#x4E2A;&#x9519;&#x8BEF;&#x3002;</p>
<p>&#x7136;&#x800C;&#xFF0C;&#x5C06;&#x65E5;&#x5FD7;&#x8BB0;&#x5F55;&#x548C;&#x5D29;&#x6E83;&#x62A5;&#x544A;&#x5206;&#x79BB;&#x662F;&#x5F88;&#x91CD;&#x8981;&#x7684;&#x3002;&#x7406;&#x60F3;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6211;&#x4EEC;&#x5E0C;&#x671B;&#x4ED6;&#x4EEC;&#x662F;&#x4E24;&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#x6A21;&#x5757;&#xFF0C;&#x4E5F;&#x53EF;&#x80FD;&#x5728;&#x4E0D;&#x540C;&#x7684;&#x5305;&#x4E2D;&#x3002;&#x5426;&#x5219;&#x6211;&#x4EEC;&#x65E0;&#x6CD5;&#x6784;&#x5EFA;&#x4E00;&#x4E2A;&#x7531;&#x8FD9;&#x4E9B;&#x5DE5;&#x5177;&#x7EC4;&#x6210;&#x7684;&#x751F;&#x6001;&#x7CFB;&#x7EDF;&#x3002;&#xFF08;&#x63D0;&#x793A;&#xFF1A;&#x6211;&#x4EEC;&#x6B63;&#x5728;&#x6162;&#x6162;&#x4E86;&#x89E3; middleware &#x7684;&#x672C;&#x8D28;&#x5230;&#x5E95;&#x662F;&#x4EC0;&#x4E48;&#xFF01;&#xFF09;</p>
<p>&#x5982;&#x679C;&#x6309;&#x7167;&#x6211;&#x4EEC;&#x7684;&#x60F3;&#x6CD5;&#xFF0C;&#x65E5;&#x5FD7;&#x8BB0;&#x5F55;&#x548C;&#x5D29;&#x6E83;&#x62A5;&#x544A;&#x5C5E;&#x4E8E;&#x4E0D;&#x540C;&#x7684;&#x6A21;&#x5757;&#xFF0C;&#x4ED6;&#x4EEC;&#x770B;&#x8D77;&#x6765;&#x5E94;&#x8BE5;&#x50CF;&#x8FD9;&#x6837;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token keyword">function</span> <span class="token function">patchStoreToAddLogging</span><span class="token punctuation">(</span>store<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> next <span class="token operator">=</span> store<span class="token punctuation">.</span>dispatch
  store<span class="token punctuation">.</span>dispatch <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token function">dispatchAndLog</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;dispatching&apos;</span><span class="token punctuation">,</span> action<span class="token punctuation">)</span>
    <span class="token keyword">let</span> result <span class="token operator">=</span> <span class="token function">next</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;next state&apos;</span><span class="token punctuation">,</span> store<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> result
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">patchStoreToAddCrashReporting</span><span class="token punctuation">(</span>store<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> next <span class="token operator">=</span> store<span class="token punctuation">.</span>dispatch
  store<span class="token punctuation">.</span>dispatch <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token function">dispatchAndReportErrors</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">try</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> <span class="token function">next</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span>
    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">err</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      console<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">&apos;&#x6355;&#x83B7;&#x4E00;&#x4E2A;&#x5F02;&#x5E38;!&apos;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
      Raven<span class="token punctuation">.</span><span class="token function">captureException</span><span class="token punctuation">(</span>err<span class="token punctuation">,</span> <span class="token punctuation">{</span>
        extra<span class="token punctuation">:</span> <span class="token punctuation">{</span>
          action<span class="token punctuation">,</span>
          state<span class="token punctuation">:</span> store<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token punctuation">}</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span>
      <span class="token keyword">throw</span> err
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5982;&#x679C;&#x8FD9;&#x4E9B;&#x529F;&#x80FD;&#x4EE5;&#x4E0D;&#x540C;&#x7684;&#x6A21;&#x5757;&#x53D1;&#x5E03;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5728; store &#x4E2D;&#x50CF;&#x8FD9;&#x6837;&#x4F7F;&#x7528;&#x5B83;&#x4EEC;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token function">patchStoreToAddLogging</span><span class="token punctuation">(</span>store<span class="token punctuation">)</span>
<span class="token function">patchStoreToAddCrashReporting</span><span class="token punctuation">(</span>store<span class="token punctuation">)</span>
</code></pre>
<p>&#x5C3D;&#x7BA1;&#x5982;&#x6B64;&#xFF0C;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x770B;&#x8D77;&#x6765;&#x8FD8;&#x662F;&#x4E0D;&#x662F;&#x591F;&#x4EE4;&#x4EBA;&#x6EE1;&#x610F;&#x3002;</p>
<h3 id="%E5%B0%9D%E8%AF%95-4-%E9%9A%90%E8%97%8F-monkeypatching">&#x5C1D;&#x8BD5; #4: &#x9690;&#x85CF; Monkeypatching</h3>
<p>Monkeypatching &#x672C;&#x8D28;&#x4E0A;&#x662F;&#x4E00;&#x79CD; hack&#x3002;&#x201C;&#x5C06;&#x4EFB;&#x610F;&#x7684;&#x65B9;&#x6CD5;&#x66FF;&#x6362;&#x6210;&#x4F60;&#x60F3;&#x8981;&#x7684;&#x201D;&#xFF0C;&#x6B64;&#x65F6;&#x7684; API &#x4F1A;&#x662F;&#x4EC0;&#x4E48;&#x6837;&#x7684;&#x5462;&#xFF1F;&#x73B0;&#x5728;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x770B;&#x8FD9;&#x79CD;&#x66FF;&#x6362;&#x7684;&#x672C;&#x8D28;&#x3002; &#x5728;&#x4E4B;&#x524D;&#xFF0C;&#x6211;&#x4EEC;&#x7528;&#x81EA;&#x5DF1;&#x7684;&#x51FD;&#x6570;&#x66FF;&#x6362;&#x6389;&#x4E86; <code>store<span class="token punctuation">.</span>dispatch</code>&#x3002;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x4E0D;&#x8FD9;&#x6837;&#x505A;&#xFF0C;&#x800C;&#x662F;&#x5728;&#x51FD;&#x6570;&#x4E2D;<strong>&#x8FD4;&#x56DE;</strong>&#x65B0;&#x7684; <code>dispatch</code> &#x5462;&#xFF1F;</p>
<pre><code class="lang-js"><span class="token keyword">function</span> <span class="token function">logger</span><span class="token punctuation">(</span>store<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> next <span class="token operator">=</span> store<span class="token punctuation">.</span>dispatch

  <span class="token comment" spellcheck="true">// &#x6211;&#x4EEC;&#x4E4B;&#x524D;&#x7684;&#x505A;&#x6CD5;:</span>
  <span class="token comment" spellcheck="true">// store.dispatch = function dispatchAndLog(action) {</span>

  <span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token function">dispatchAndLog</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;dispatching&apos;</span><span class="token punctuation">,</span> action<span class="token punctuation">)</span>
    <span class="token keyword">let</span> result <span class="token operator">=</span> <span class="token function">next</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;next state&apos;</span><span class="token punctuation">,</span> store<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> result
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5728; Redux &#x5185;&#x90E8;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x53EF;&#x4EE5;&#x5C06;&#x5B9E;&#x9645;&#x7684; monkeypatching &#x5E94;&#x7528;&#x5230; <code>store<span class="token punctuation">.</span>dispatch</code> &#x4E2D;&#x7684;&#x8F85;&#x52A9;&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token keyword">function</span> <span class="token function">applyMiddlewareByMonkeypatching</span><span class="token punctuation">(</span>store<span class="token punctuation">,</span> middlewares<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  middlewares <span class="token operator">=</span> middlewares<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  middlewares<span class="token punctuation">.</span><span class="token function">reverse</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

  <span class="token comment" spellcheck="true">// &#x5728;&#x6BCF;&#x4E00;&#x4E2A; middleware &#x4E2D;&#x53D8;&#x6362; dispatch &#x65B9;&#x6CD5;&#x3002;</span>
  middlewares<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>middleware <span class="token operator">=</span><span class="token operator">&gt;</span>
    store<span class="token punctuation">.</span>dispatch <span class="token operator">=</span> <span class="token function">middleware</span><span class="token punctuation">(</span>store<span class="token punctuation">)</span>
  <span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x7136;&#x540E;&#x50CF;&#x8FD9;&#x6837;&#x5E94;&#x7528;&#x591A;&#x4E2A; middleware&#xFF1A;</p>
<pre><code class="lang-js"><span class="token function">applyMiddlewareByMonkeypatching</span><span class="token punctuation">(</span>store<span class="token punctuation">,</span> <span class="token punctuation">[</span> logger<span class="token punctuation">,</span> crashReporter <span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre>
<p>&#x5C3D;&#x7BA1;&#x6211;&#x4EEC;&#x505A;&#x4E86;&#x5F88;&#x591A;&#xFF0C;&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;&#x4F9D;&#x65E7;&#x662F; monkeypatching&#x3002;<br>&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x4EC5;&#x4EC5;&#x662F;&#x5C06;&#x5B83;&#x9690;&#x85CF;&#x5728;&#x6211;&#x4EEC;&#x7684;&#x6846;&#x67B6;&#x5185;&#x90E8;&#xFF0C;&#x5E76;&#x6CA1;&#x6709;&#x6539;&#x53D8;&#x8FD9;&#x4E2A;&#x4E8B;&#x5B9E;&#x3002;</p>
<h3 id="%E5%B0%9D%E8%AF%95-5-%E7%A7%BB%E9%99%A4-monkeypatching">&#x5C1D;&#x8BD5; #5: &#x79FB;&#x9664; Monkeypatching</h3>
<p>&#x4E3A;&#x4EC0;&#x4E48;&#x6211;&#x4EEC;&#x8981;&#x66FF;&#x6362;&#x539F;&#x6765;&#x7684; <code>dispatch</code> &#x5462;&#xFF1F;&#x5F53;&#x7136;&#xFF0C;&#x8FD9;&#x6837;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x5728;&#x540E;&#x9762;&#x76F4;&#x63A5;&#x8C03;&#x7528;&#x5B83;&#xFF0C;&#x4F46;&#x662F;&#x8FD8;&#x6709;&#x53E6;&#x4E00;&#x4E2A;&#x539F;&#x56E0;&#xFF1A;&#x5C31;&#x662F;&#x6BCF;&#x4E00;&#x4E2A; middleware &#x90FD;&#x53EF;&#x4EE5;&#x64CD;&#x4F5C;&#xFF08;&#x6216;&#x8005;&#x76F4;&#x63A5;&#x8C03;&#x7528;&#xFF09;&#x524D;&#x4E00;&#x4E2A; middleware &#x5305;&#x88C5;&#x8FC7;&#x7684; <code>store<span class="token punctuation">.</span>dispatch</code>&#xFF1A;</p>
<pre><code class="lang-js"><span class="token keyword">function</span> <span class="token function">logger</span><span class="token punctuation">(</span>store<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment" spellcheck="true">// &#x8FD9;&#x91CC;&#x7684; next &#x5FC5;&#x987B;&#x6307;&#x5411;&#x524D;&#x4E00;&#x4E2A; middleware &#x8FD4;&#x56DE;&#x7684;&#x51FD;&#x6570;&#xFF1A;</span>
  <span class="token keyword">let</span> next <span class="token operator">=</span> store<span class="token punctuation">.</span>dispatch

  <span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token function">dispatchAndLog</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;dispatching&apos;</span><span class="token punctuation">,</span> action<span class="token punctuation">)</span>
    <span class="token keyword">let</span> result <span class="token operator">=</span> <span class="token function">next</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;next state&apos;</span><span class="token punctuation">,</span> store<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> result
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5C06; middleware &#x4E32;&#x8FDE;&#x8D77;&#x6765;&#x7684;&#x5FC5;&#x8981;&#x6027;&#x662F;&#x663E;&#x800C;&#x6613;&#x89C1;&#x7684;&#x3002;</p>
<p>&#x5982;&#x679C; <code>applyMiddlewareByMonkeypatching</code> &#x65B9;&#x6CD5;&#x4E2D;&#x6CA1;&#x6709;&#x5728;&#x7B2C;&#x4E00;&#x4E2A; middleware &#x6267;&#x884C;&#x65F6;&#x7ACB;&#x5373;&#x66FF;&#x6362;&#x6389; <code>store<span class="token punctuation">.</span>dispatch</code>&#xFF0C;&#x90A3;&#x4E48; <code>store<span class="token punctuation">.</span>dispatch</code> &#x5C06;&#x4F1A;&#x4E00;&#x76F4;&#x6307;&#x5411;&#x539F;&#x59CB;&#x7684; <code>dispatch</code> &#x65B9;&#x6CD5;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A; middleware &#x4F9D;&#x65E7;&#x4F1A;&#x4F5C;&#x7528;&#x5728;&#x539F;&#x59CB;&#x7684; <code>dispatch</code> &#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x4F46;&#x662F;&#xFF0C;&#x8FD8;&#x6709;&#x53E6;&#x4E00;&#x79CD;&#x65B9;&#x5F0F;&#x6765;&#x5B9E;&#x73B0;&#x8FD9;&#x79CD;&#x94FE;&#x5F0F;&#x8C03;&#x7528;&#x7684;&#x6548;&#x679C;&#x3002;&#x53EF;&#x4EE5;&#x8BA9; middleware &#x4EE5;&#x65B9;&#x6CD5;&#x53C2;&#x6570;&#x7684;&#x5F62;&#x5F0F;&#x63A5;&#x6536;&#x4E00;&#x4E2A; <code><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code> &#x65B9;&#x6CD5;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x901A;&#x8FC7; store &#x7684;&#x5B9E;&#x4F8B;&#x53BB;&#x83B7;&#x53D6;&#x3002;</p>
<pre><code class="lang-js"><span class="token keyword">function</span> <span class="token function">logger</span><span class="token punctuation">(</span>store<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token function">wrapDispatchToAddLogging</span><span class="token punctuation">(</span>next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token function">dispatchAndLog</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;dispatching&apos;</span><span class="token punctuation">,</span> action<span class="token punctuation">)</span>
      <span class="token keyword">let</span> result <span class="token operator">=</span> <span class="token function">next</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;next state&apos;</span><span class="token punctuation">,</span> store<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
      <span class="token keyword">return</span> result
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x73B0;&#x5728;&#x662F;<a href="http://knowyourmeme.com/memes/we-need-to-go-deeper" target="_blank">&#x201C;&#x6211;&#x4EEC;&#x8BE5;&#x66F4;&#x8FDB;&#x4E00;&#x6B65;&#x201D;</a>&#x7684;&#x65F6;&#x523B;&#x4E86;&#xFF0C;&#x6240;&#x4EE5;&#x53EF;&#x80FD;&#x4F1A;&#x591A;&#x82B1;&#x4E00;&#x70B9;&#x65F6;&#x95F4;&#x6765;&#x8BA9;&#x5B83;&#x53D8;&#x7684;&#x66F4;&#x4E3A;&#x5408;&#x7406;&#x4E00;&#x4E9B;&#x3002;&#x8FD9;&#x4E9B;&#x4E32;&#x8054;&#x51FD;&#x6570;&#x5F88;&#x5413;&#x4EBA;&#x3002;ES6 &#x7684;&#x7BAD;&#x5934;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x4F7F;&#x5176; <a href="https://en.wikipedia.org/wiki/Currying" target="_blank">&#x67EF;&#x91CC;&#x5316;</a> &#xFF0C;&#x4ECE;&#x800C;&#x770B;&#x8D77;&#x6765;&#x66F4;&#x8212;&#x670D;&#x4E00;&#x4E9B;:</p>
<pre><code class="lang-js"><span class="token keyword">const</span> logger <span class="token operator">=</span> store <span class="token operator">=</span><span class="token operator">&gt;</span> next <span class="token operator">=</span><span class="token operator">&gt;</span> action <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;dispatching&apos;</span><span class="token punctuation">,</span> action<span class="token punctuation">)</span>
  <span class="token keyword">let</span> result <span class="token operator">=</span> <span class="token function">next</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;next state&apos;</span><span class="token punctuation">,</span> store<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token keyword">return</span> result
<span class="token punctuation">}</span>

<span class="token keyword">const</span> crashReporter <span class="token operator">=</span> store <span class="token operator">=</span><span class="token operator">&gt;</span> next <span class="token operator">=</span><span class="token operator">&gt;</span> action <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">try</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">next</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span>
  <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">err</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">&apos;Caught an exception!&apos;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
    Raven<span class="token punctuation">.</span><span class="token function">captureException</span><span class="token punctuation">(</span>err<span class="token punctuation">,</span> <span class="token punctuation">{</span>
      extra<span class="token punctuation">:</span> <span class="token punctuation">{</span>
        action<span class="token punctuation">,</span>
        state<span class="token punctuation">:</span> store<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token keyword">throw</span> err
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p><strong>&#x8FD9;&#x6B63;&#x662F; Redux middleware &#x7684;&#x6837;&#x5B50;&#x3002;</strong></p>
<p>Middleware &#x63A5;&#x6536;&#x4E86;&#x4E00;&#x4E2A; <code><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code> &#x7684; dispatch &#x51FD;&#x6570;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A; dispatch &#x51FD;&#x6570;&#xFF0C;&#x8FD4;&#x56DE;&#x7684;&#x51FD;&#x6570;&#x4F1A;&#x88AB;&#x4F5C;&#x4E3A;&#x4E0B;&#x4E00;&#x4E2A; middleware &#x7684; <code><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>&#xFF0C;&#x4EE5;&#x6B64;&#x7C7B;&#x63A8;&#x3002;&#x7531;&#x4E8E; store &#x4E2D;&#x7C7B;&#x4F3C; <code><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code> &#x7684;&#x65B9;&#x6CD5;&#x4F9D;&#x65E7;&#x975E;&#x5E38;&#x6709;&#x7528;&#xFF0C;&#x6211;&#x4EEC;&#x5C06; <code>store</code> &#x4F5C;&#x4E3A;&#x9876;&#x5C42;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x4F7F;&#x5F97;&#x5B83;&#x53EF;&#x4EE5;&#x5728;&#x6240;&#x6709; middleware &#x4E2D;&#x88AB;&#x4F7F;&#x7528;&#x3002;</p>
<h3 id="%E5%B0%9D%E8%AF%95-6-%E2%80%9C%E5%8D%95%E7%BA%AF%E2%80%9D%E5%9C%B0%E4%BD%BF%E7%94%A8-middleware">&#x5C1D;&#x8BD5; #6: &#x201C;&#x5355;&#x7EAF;&#x201D;&#x5730;&#x4F7F;&#x7528; Middleware</h3>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5199;&#x4E00;&#x4E2A; <code><span class="token function">applyMiddleware</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code> &#x65B9;&#x6CD5;&#x66FF;&#x6362;&#x6389;&#x539F;&#x6765;&#x7684; <code><span class="token function">applyMiddlewareByMonkeypatching</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>&#x3002;&#x5728;&#x65B0;&#x7684; <code><span class="token function">applyMiddleware</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code> &#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x53D6;&#x5F97;&#x6700;&#x7EC8;&#x5B8C;&#x6574;&#x7684;&#x88AB;&#x5305;&#x88C5;&#x8FC7;&#x7684; <code><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code> &#x51FD;&#x6570;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A; store &#x7684;&#x526F;&#x672C;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token comment" spellcheck="true">// &#x8B66;&#x544A;&#xFF1A;&#x8FD9;&#x53EA;&#x662F;&#x4E00;&#x79CD;&#x201C;&#x5355;&#x7EAF;&#x201D;&#x7684;&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;&#xFF01;</span>
<span class="token comment" spellcheck="true">// &#x8FD9; *&#x5E76;&#x4E0D;&#x662F;* Redux &#x7684; API.</span>

<span class="token keyword">function</span> <span class="token function">applyMiddleware</span><span class="token punctuation">(</span>store<span class="token punctuation">,</span> middlewares<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  middlewares <span class="token operator">=</span> middlewares<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  middlewares<span class="token punctuation">.</span><span class="token function">reverse</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

  <span class="token keyword">let</span> dispatch <span class="token operator">=</span> store<span class="token punctuation">.</span>dispatch
  middlewares<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span>middleware <span class="token operator">=</span><span class="token operator">&gt;</span>
    dispatch <span class="token operator">=</span> <span class="token function">middleware</span><span class="token punctuation">(</span>store<span class="token punctuation">)</span><span class="token punctuation">(</span>dispatch<span class="token punctuation">)</span>
  <span class="token punctuation">)</span>

  <span class="token keyword">return</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> store<span class="token punctuation">,</span> <span class="token punctuation">{</span> dispatch <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD9;&#x4E0E; Redux &#x4E2D; <a href="../api/applyMiddleware.html"><code><span class="token function">applyMiddleware</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code></a> &#x7684;&#x5B9E;&#x73B0;&#x5DF2;&#x7ECF;&#x5F88;&#x63A5;&#x8FD1;&#x4E86;&#xFF0C;&#x4F46;&#x662F;<strong>&#x6709;&#x4E09;&#x4E2A;&#x91CD;&#x8981;&#x7684;&#x4E0D;&#x540C;&#x4E4B;&#x5904;</strong>&#xFF1A;</p>
<ul>
<li><p>&#x5B83;&#x53EA;&#x66B4;&#x9732;&#x4E00;&#x4E2A; <a href="../api/Store.html">store API</a> &#x7684;&#x5B50;&#x96C6;&#x7ED9; middleware&#xFF1A;<a href="../api/Store.html#dispatch"><code><span class="token function">dispatch</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span></code></a> &#x548C; <a href="../api/Store.html#getState"><code><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code></a>&#x3002;</p>
</li>
<li><p>&#x5B83;&#x7528;&#x4E86;&#x4E00;&#x4E2A;&#x975E;&#x5E38;&#x5DE7;&#x5999;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x4EE5;&#x786E;&#x4FDD;&#x5982;&#x679C;&#x4F60;&#x5728; middleware &#x4E2D;&#x8C03;&#x7528;&#x7684;&#x662F; <code>store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span></code> &#x800C;&#x4E0D;&#x662F; <code><span class="token function">next</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span></code>&#xFF0C;&#x90A3;&#x4E48;&#x8FD9;&#x4E2A;&#x64CD;&#x4F5C;&#x4F1A;&#x518D;&#x6B21;&#x904D;&#x5386;&#x5305;&#x542B;&#x5F53;&#x524D; middleware &#x5728;&#x5185;&#x7684;&#x6574;&#x4E2A; middleware &#x94FE;&#x3002;&#x8FD9;&#x5BF9;&#x5F02;&#x6B65;&#x7684; middleware &#x975E;&#x5E38;&#x6709;&#x7528;&#xFF0C;&#x6B63;&#x5982;&#x6211;&#x4EEC;&#x5728;<a href="AsyncActions.html">&#x4E4B;&#x524D;&#x7684;&#x7AE0;&#x8282;</a>&#x4E2D;&#x63D0;&#x5230;&#x7684;&#x3002;</p>
</li>
<li><p>&#x4E3A;&#x4E86;&#x4FDD;&#x8BC1;&#x4F60;&#x53EA;&#x80FD;&#x5E94;&#x7528; middleware &#x4E00;&#x6B21;&#xFF0C;&#x5B83;&#x4F5C;&#x7528;&#x5728; <code><span class="token function">createStore</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code> &#x4E0A;&#x800C;&#x4E0D;&#x662F; <code>store</code> &#x672C;&#x8EAB;&#x3002;&#x56E0;&#x6B64;&#x5B83;&#x7684;&#x7B7E;&#x540D;&#x4E0D;&#x662F; <code><span class="token punctuation">(</span>store<span class="token punctuation">,</span> middlewares<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> store</code>&#xFF0C; &#x800C;&#x662F; <code><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>middlewares<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">(</span>createStore<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> createStore</code>&#x3002;</p>
</li>
</ul>
<p>&#x7531;&#x4E8E;&#x5728;&#x4F7F;&#x7528;&#x4E4B;&#x524D;&#x9700;&#x8981;&#x5148;&#x5E94;&#x7528;&#x65B9;&#x6CD5;&#x5230; <code><span class="token function">createStore</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code> &#x4E4B;&#x4E0A;&#x6709;&#x4E9B;&#x9EBB;&#x70E6;&#xFF0C;<code><span class="token function">createStore</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code> &#x4E5F;&#x63A5;&#x53D7;&#x5C06;&#x5E0C;&#x671B;&#x88AB;&#x5E94;&#x7528;&#x7684;&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x53C2;&#x6570;&#x4F20;&#x5165;&#x3002;</p>
<h3 id="%E6%9C%80%E7%BB%88%E7%9A%84%E6%96%B9%E6%B3%95">&#x6700;&#x7EC8;&#x7684;&#x65B9;&#x6CD5;</h3>
<p>&#x8FD9;&#x662F;&#x6211;&#x4EEC;&#x521A;&#x521A;&#x6240;&#x5199;&#x7684; middleware&#xFF1A;</p>
<pre><code class="lang-js"><span class="token keyword">const</span> logger <span class="token operator">=</span> store <span class="token operator">=</span><span class="token operator">&gt;</span> next <span class="token operator">=</span><span class="token operator">&gt;</span> action <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;dispatching&apos;</span><span class="token punctuation">,</span> action<span class="token punctuation">)</span>
  <span class="token keyword">let</span> result <span class="token operator">=</span> <span class="token function">next</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;next state&apos;</span><span class="token punctuation">,</span> store<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token keyword">return</span> result
<span class="token punctuation">}</span>

<span class="token keyword">const</span> crashReporter <span class="token operator">=</span> store <span class="token operator">=</span><span class="token operator">&gt;</span> next <span class="token operator">=</span><span class="token operator">&gt;</span> action <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">try</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">next</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span>
  <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">err</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">&apos;Caught an exception!&apos;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
    Raven<span class="token punctuation">.</span><span class="token function">captureException</span><span class="token punctuation">(</span>err<span class="token punctuation">,</span> <span class="token punctuation">{</span>
      extra<span class="token punctuation">:</span> <span class="token punctuation">{</span>
        action<span class="token punctuation">,</span>
        state<span class="token punctuation">:</span> store<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token keyword">throw</span> err
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x7136;&#x540E;&#x662F;&#x5C06;&#x5B83;&#x4EEC;&#x5F15;&#x7528;&#x5230; Redux store &#x4E2D;&#xFF1A;</p>
<pre><code class="lang-js"><span class="token keyword">import</span> <span class="token punctuation">{</span> createStore<span class="token punctuation">,</span> combineReducers<span class="token punctuation">,</span> applyMiddleware <span class="token punctuation">}</span> from <span class="token string">&apos;redux&apos;</span>

<span class="token keyword">let</span> todoApp <span class="token operator">=</span> <span class="token function">combineReducers</span><span class="token punctuation">(</span>reducers<span class="token punctuation">)</span>
<span class="token keyword">let</span> store <span class="token operator">=</span> <span class="token function">createStore</span><span class="token punctuation">(</span>
  todoApp<span class="token punctuation">,</span>
  <span class="token comment" spellcheck="true">// applyMiddleware() &#x544A;&#x8BC9; createStore() &#x5982;&#x4F55;&#x5904;&#x7406;&#x4E2D;&#x95F4;&#x4EF6;</span>
  <span class="token function">applyMiddleware</span><span class="token punctuation">(</span>logger<span class="token punctuation">,</span> crashReporter<span class="token punctuation">)</span>
<span class="token punctuation">)</span>
</code></pre>
<p>&#x5C31;&#x662F;&#x8FD9;&#x6837;&#xFF01;&#x73B0;&#x5728;&#x4EFB;&#x4F55;&#x88AB;&#x53D1;&#x9001;&#x5230; store &#x7684; action &#x90FD;&#x4F1A;&#x7ECF;&#x8FC7; <code>logger</code> &#x548C; <code>crashReporter</code>&#xFF1A;</p>
<pre><code class="lang-js"><span class="token comment" spellcheck="true">// &#x5C06;&#x7ECF;&#x8FC7; logger &#x548C; crashReporter &#x4E24;&#x4E2A; middleware&#xFF01;</span>
store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token function">addTodo</span><span class="token punctuation">(</span><span class="token string">&apos;Use Redux&apos;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre>
<h2 id="7%E4%B8%AA%E7%A4%BA%E4%BE%8B">7&#x4E2A;&#x793A;&#x4F8B;</h2>
<p>&#x5982;&#x679C;&#x8BFB;&#x5B8C;&#x4E0A;&#x9762;&#x7684;&#x7AE0;&#x8282;&#x4F60;&#x5DF2;&#x7ECF;&#x89C9;&#x5F97;&#x5934;&#x90FD;&#x8981;&#x7206;&#x4E86;&#xFF0C;&#x90A3;&#x5C31;&#x60F3;&#x8C61;&#x4E00;&#x4E0B;&#x628A;&#x5B83;&#x5199;&#x51FA;&#x6765;&#x4E4B;&#x540E;&#x7684;&#x6837;&#x5B50;&#x3002;&#x4E0B;&#x9762;&#x7684;&#x5185;&#x5BB9;&#x4F1A;&#x8BA9;&#x6211;&#x4EEC;&#x653E;&#x677E;&#x4E00;&#x4E0B;&#xFF0C;&#x5E76;&#x8BA9;&#x4F60;&#x7684;&#x601D;&#x8DEF;&#x5EF6;&#x7EED;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x7684;&#x6BCF;&#x4E2A;&#x51FD;&#x6570;&#x90FD;&#x662F;&#x4E00;&#x4E2A;&#x6709;&#x6548;&#x7684; Redux middleware&#x3002;&#x5B83;&#x4EEC;&#x4E0D;&#x662F;&#x540C;&#x6837;&#x6709;&#x7528;&#xFF0C;&#x4F46;&#x662F;&#x81F3;&#x5C11;&#x4ED6;&#x4EEC;&#x4E00;&#x6837;&#x6709;&#x8DA3;&#x3002;</p>
<pre><code class="lang-js"><span class="token comment" spellcheck="true">/**
 * &#x8BB0;&#x5F55;&#x6240;&#x6709;&#x88AB;&#x53D1;&#x8D77;&#x7684; action &#x4EE5;&#x53CA;&#x4EA7;&#x751F;&#x7684;&#x65B0;&#x7684; state&#x3002;
 */</span>
<span class="token keyword">const</span> logger <span class="token operator">=</span> store <span class="token operator">=</span><span class="token operator">&gt;</span> next <span class="token operator">=</span><span class="token operator">&gt;</span> action <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">group</span><span class="token punctuation">(</span>action<span class="token punctuation">.</span>type<span class="token punctuation">)</span>
  console<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&apos;dispatching&apos;</span><span class="token punctuation">,</span> action<span class="token punctuation">)</span>
  <span class="token keyword">let</span> result <span class="token operator">=</span> <span class="token function">next</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;next state&apos;</span><span class="token punctuation">,</span> store<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  console<span class="token punctuation">.</span><span class="token function">groupEnd</span><span class="token punctuation">(</span>action<span class="token punctuation">.</span>type<span class="token punctuation">)</span>
  <span class="token keyword">return</span> result
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">/**
 * &#x5728; state &#x66F4;&#x65B0;&#x5B8C;&#x6210;&#x548C; listener &#x88AB;&#x901A;&#x77E5;&#x4E4B;&#x540E;&#x53D1;&#x9001;&#x5D29;&#x6E83;&#x62A5;&#x544A;&#x3002;
 */</span>
<span class="token keyword">const</span> crashReporter <span class="token operator">=</span> store <span class="token operator">=</span><span class="token operator">&gt;</span> next <span class="token operator">=</span><span class="token operator">&gt;</span> action <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">try</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">next</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span>
  <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">err</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">&apos;Caught an exception!&apos;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
    Raven<span class="token punctuation">.</span><span class="token function">captureException</span><span class="token punctuation">(</span>err<span class="token punctuation">,</span> <span class="token punctuation">{</span>
      extra<span class="token punctuation">:</span> <span class="token punctuation">{</span>
        action<span class="token punctuation">,</span>
        state<span class="token punctuation">:</span> store<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token keyword">throw</span> err
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">/**
 * &#x7528; { meta: { delay: N } } &#x6765;&#x8BA9; action &#x5EF6;&#x8FDF; N &#x6BEB;&#x79D2;&#x3002;
 * &#x5728;&#x8FD9;&#x4E2A;&#x6848;&#x4F8B;&#x4E2D;&#xFF0C;&#x8BA9; `dispatch` &#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x53D6;&#x6D88; timeout &#x7684;&#x51FD;&#x6570;&#x3002;
 */</span>
<span class="token keyword">const</span> timeoutScheduler <span class="token operator">=</span> store <span class="token operator">=</span><span class="token operator">&gt;</span> next <span class="token operator">=</span><span class="token operator">&gt;</span> action <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>action<span class="token punctuation">.</span>meta <span class="token operator">||</span> <span class="token operator">!</span>action<span class="token punctuation">.</span>meta<span class="token punctuation">.</span>delay<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">next</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">let</span> timeoutId <span class="token operator">=</span> <span class="token function">setTimeout</span><span class="token punctuation">(</span>
    <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">next</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span><span class="token punctuation">,</span>
    action<span class="token punctuation">.</span>meta<span class="token punctuation">.</span>delay
  <span class="token punctuation">)</span>

  <span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token function">cancel</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">clearTimeout</span><span class="token punctuation">(</span>timeoutId<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">/**
 * &#x901A;&#x8FC7; { meta: { raf: true } } &#x8BA9; action &#x5728;&#x4E00;&#x4E2A; rAF &#x5FAA;&#x73AF;&#x5E27;&#x4E2D;&#x88AB;&#x53D1;&#x8D77;&#x3002;
 * &#x5728;&#x8FD9;&#x4E2A;&#x6848;&#x4F8B;&#x4E2D;&#xFF0C;&#x8BA9; `dispatch` &#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x4ECE;&#x961F;&#x5217;&#x4E2D;&#x79FB;&#x9664;&#x8BE5; action &#x7684;&#x51FD;&#x6570;&#x3002;
 */</span>
<span class="token keyword">const</span> rafScheduler <span class="token operator">=</span> store <span class="token operator">=</span><span class="token operator">&gt;</span> next <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> queuedActions <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  <span class="token keyword">let</span> frame <span class="token operator">=</span> <span class="token keyword">null</span>

  <span class="token keyword">function</span> <span class="token function">loop</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    frame <span class="token operator">=</span> <span class="token keyword">null</span>
    <span class="token keyword">try</span> <span class="token punctuation">{</span>
      <span class="token keyword">if</span> <span class="token punctuation">(</span>queuedActions<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">next</span><span class="token punctuation">(</span>queuedActions<span class="token punctuation">.</span><span class="token function">shift</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
      <span class="token function">maybeRaf</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">function</span> <span class="token function">maybeRaf</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>queuedActions<span class="token punctuation">.</span>length <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span>frame<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      frame <span class="token operator">=</span> <span class="token function">requestAnimationFrame</span><span class="token punctuation">(</span>loop<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">return</span> action <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>action<span class="token punctuation">.</span>meta <span class="token operator">||</span> <span class="token operator">!</span>action<span class="token punctuation">.</span>meta<span class="token punctuation">.</span>raf<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> <span class="token function">next</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    queuedActions<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span>
    <span class="token function">maybeRaf</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token function">cancel</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      queuedActions <span class="token operator">=</span> queuedActions<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>a <span class="token operator">=</span><span class="token operator">&gt;</span> a <span class="token operator">!</span><span class="token operator">==</span> action<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">/**
 * &#x4F7F;&#x4F60;&#x9664;&#x4E86; action &#x4E4B;&#x5916;&#x8FD8;&#x53EF;&#x4EE5;&#x53D1;&#x8D77; promise&#x3002;
 * &#x5982;&#x679C;&#x8FD9;&#x4E2A; promise &#x88AB; resolved&#xFF0C;&#x4ED6;&#x7684;&#x7ED3;&#x679C;&#x5C06;&#x88AB;&#x4F5C;&#x4E3A; action &#x53D1;&#x8D77;&#x3002;
 * &#x8FD9;&#x4E2A; promise &#x4F1A;&#x88AB; `dispatch` &#x8FD4;&#x56DE;&#xFF0C;&#x56E0;&#x6B64;&#x8C03;&#x7528;&#x8005;&#x53EF;&#x4EE5;&#x5904;&#x7406; rejection&#x3002;
 */</span>
<span class="token keyword">const</span> vanillaPromise <span class="token operator">=</span> store <span class="token operator">=</span><span class="token operator">&gt;</span> next <span class="token operator">=</span><span class="token operator">&gt;</span> action <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span> action<span class="token punctuation">.</span>then <span class="token operator">!</span><span class="token operator">==</span> <span class="token string">&apos;function&apos;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">next</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">return</span> Promise<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>store<span class="token punctuation">.</span>dispatch<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">/**
 * &#x8BA9;&#x4F60;&#x53EF;&#x4EE5;&#x53D1;&#x8D77;&#x5E26;&#x6709;&#x4E00;&#x4E2A; { promise } &#x5C5E;&#x6027;&#x7684;&#x7279;&#x6B8A; action&#x3002;
 *
 * &#x8FD9;&#x4E2A; middleware &#x4F1A;&#x5728;&#x5F00;&#x59CB;&#x65F6;&#x53D1;&#x8D77;&#x4E00;&#x4E2A; action&#xFF0C;&#x5E76;&#x5728;&#x8FD9;&#x4E2A; `promise` resolve &#x65F6;&#x53D1;&#x8D77;&#x53E6;&#x4E00;&#x4E2A;&#x6210;&#x529F;&#xFF08;&#x6216;&#x5931;&#x8D25;&#xFF09;&#x7684; action&#x3002;
 *
 * &#x4E3A;&#x4E86;&#x65B9;&#x4FBF;&#x8D77;&#x89C1;&#xFF0C;`dispatch` &#x4F1A;&#x8FD4;&#x56DE;&#x8FD9;&#x4E2A; promise &#x8BA9;&#x8C03;&#x7528;&#x8005;&#x53EF;&#x4EE5;&#x7B49;&#x5F85;&#x3002;
 */</span>
<span class="token keyword">const</span> readyStatePromise <span class="token operator">=</span> store <span class="token operator">=</span><span class="token operator">&gt;</span> next <span class="token operator">=</span><span class="token operator">&gt;</span> action <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>action<span class="token punctuation">.</span>promise<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token function">next</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">function</span> <span class="token function">makeAction</span><span class="token punctuation">(</span>ready<span class="token punctuation">,</span> data<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> newAction <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> action<span class="token punctuation">,</span> <span class="token punctuation">{</span> ready <span class="token punctuation">}</span><span class="token punctuation">,</span> data<span class="token punctuation">)</span>
    <span class="token keyword">delete</span> newAction<span class="token punctuation">.</span>promise
    <span class="token keyword">return</span> newAction
  <span class="token punctuation">}</span>

  <span class="token function">next</span><span class="token punctuation">(</span><span class="token function">makeAction</span><span class="token punctuation">(</span><span class="token keyword">false</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token keyword">return</span> action<span class="token punctuation">.</span>promise<span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>
    result <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">next</span><span class="token punctuation">(</span><span class="token function">makeAction</span><span class="token punctuation">(</span><span class="token keyword">true</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> result <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    error <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token function">next</span><span class="token punctuation">(</span><span class="token function">makeAction</span><span class="token punctuation">(</span><span class="token keyword">true</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> error <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">/**
 * &#x8BA9;&#x4F60;&#x53EF;&#x4EE5;&#x53D1;&#x8D77;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x6765;&#x66FF;&#x4EE3; action&#x3002;
 * &#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x63A5;&#x6536; `dispatch` &#x548C; `getState` &#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x3002;
 *
 * &#x5BF9;&#x4E8E;&#xFF08;&#x6839;&#x636E; `getState()` &#x7684;&#x60C5;&#x51B5;&#xFF09;&#x63D0;&#x524D;&#x9000;&#x51FA;&#xFF0C;&#x6216;&#x8005;&#x5F02;&#x6B65;&#x63A7;&#x5236;&#x6D41;&#xFF08; `dispatch()` &#x4E00;&#x4E9B;&#x5176;&#x4ED6;&#x4E1C;&#x897F;&#xFF09;&#x6765;&#x8BF4;&#xFF0C;&#x8FD9;&#x975E;&#x5E38;&#x6709;&#x7528;&#x3002;
 *
 * `dispatch` &#x4F1A;&#x8FD4;&#x56DE;&#x88AB;&#x53D1;&#x8D77;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x3002;
 */</span>
<span class="token keyword">const</span> thunk <span class="token operator">=</span> store <span class="token operator">=</span><span class="token operator">&gt;</span> next <span class="token operator">=</span><span class="token operator">&gt;</span> action <span class="token operator">=</span><span class="token operator">&gt;</span>
  <span class="token keyword">typeof</span> action <span class="token operator">===</span> <span class="token string">&apos;function&apos;</span> <span class="token operator">?</span>
    <span class="token function">action</span><span class="token punctuation">(</span>store<span class="token punctuation">.</span>dispatch<span class="token punctuation">,</span> store<span class="token punctuation">.</span>getState<span class="token punctuation">)</span> <span class="token punctuation">:</span>
    <span class="token function">next</span><span class="token punctuation">(</span>action<span class="token punctuation">)</span>

<span class="token comment" spellcheck="true">// &#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4EE5;&#x4E0A;&#x5168;&#x90E8;&#x7684; middleware&#xFF01;&#xFF08;&#x5F53;&#x7136;&#xFF0C;&#x8FD9;&#x4E0D;&#x610F;&#x5473;&#x7740;&#x4F60;&#x5FC5;&#x987B;&#x5168;&#x90FD;&#x4F7F;&#x7528;&#x3002;&#xFF09;</span>
<span class="token keyword">let</span> todoApp <span class="token operator">=</span> <span class="token function">combineReducers</span><span class="token punctuation">(</span>reducers<span class="token punctuation">)</span>
<span class="token keyword">let</span> store <span class="token operator">=</span> <span class="token function">createStore</span><span class="token punctuation">(</span>
  todoApp<span class="token punctuation">,</span>
  <span class="token function">applyMiddleware</span><span class="token punctuation">(</span>
    rafScheduler<span class="token punctuation">,</span>
    timeoutScheduler<span class="token punctuation">,</span>
    thunk<span class="token punctuation">,</span>
    vanillaPromise<span class="token punctuation">,</span>
    readyStatePromise<span class="token punctuation">,</span>
    logger<span class="token punctuation">,</span>
    crashReporter
  <span class="token punctuation">)</span>
<span class="token punctuation">)</span>
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../docs/advanced/AsyncFlow.html" class="navigation navigation-prev " aria-label="Previous page: 异步数据流"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../docs/advanced/UsageWithReactRouter.html" class="navigation navigation-next " aria-label="Next page: 搭配 React Router"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-edit-link/plugin.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-ga/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2},"edit-link":{"base":"https://github.com/camsong/redux-in-chinese/tree/master","label":"开始纠错"},"ga":{"token":"UA-66122997-1","configuration":"auto"}};
    gitbook.start(config);
});
</script>

        <script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');ga('create', 'UA-66122997-1', 'auto');ga('send', 'pageview');</script>
    </body>
    
</html>
